<?php

namespace app\queue;

use app\common\exception\FailedException;
use app\common\library\Code;
use Webman\RedisQueue\Consumer;

class BaseJob implements Consumer
{
    public $queue = '';

    // 连接名，对应 plugin/webman/redis-queue/redis.php 里的连接`
    public $connection = 'webman_gii';

    public function consume($data)
    {
        $log = [
            'job'   => $this->queue,
            'connection'   => $this->connection,
            'job_id'=> $data['job_id'] ?? '',
            'param' => json_encode($data),
        ];
        //执行业务处理
        $result = $this->doJob($data);
        $log['result_message'] = $result['msg'];
        if($result['code'] == Code::SUCCESS){
            $log['result'] = $result['data'] ? json_encode($result['data'],JSON_UNESCAPED_UNICODE) : 1;
        }
//        var_export($log);//
        if (!in_array($this->queue, ['Health', 'CloseClient'])) {
            //QueueLog::create($log);
        }
        //触发重试机制
        if ($result['code'] == Code::QUEUE_FAILED) {
//            throw new FailedException('主动抛出异常，触发重试机制');
        }
    }

    public static function checkResult($result)
    {
        if ($result['code'] != Code::SUCCESS) {
            throw new FailedException($result['msg'], $result['code']);
        }
    }
}